﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BridgeFloor : MonoBehaviour
{
    public enum EnumBridgeFloorType
    {
        Left = 0,
        Middle,
        Right,
    }
    public enum EnumState
    {
        Complete = 0,
        Booming,
        Residual,
    }
    [System.Serializable]
    public struct Config
    {
        [Tooltip("桥面类型")]
        public EnumBridgeFloorType bridgeFloorType;
        [Tooltip("桥面完整素材")]
        public Sprite sprite;
        [Tooltip("桥面爆炸素材")]
        public Sprite boomSprite;
        [Tooltip("桥面残留素材")]
        public Sprite residualSprite;
    }

    [Header("基础配置")]
    public Config[] bridgeFloorConfig;
    public EnumBridgeFloorType bridgeFloorType;
    public EnumState bridgeFloorState;

    [Header("爆炸配置")]
    public GameObject boomPrefab;
    public float completeTime = 4f / 60f;
    public float boomingTime = 52f / 60f;
    public int boomCount = 5;
    public float boomIntervalTime = 2f / 60f;

    private bool booming;
    private float startBoomTime;
    private int haveBoomCount = 0;
    private SpriteRenderer sr;
    private bool playedDeathSoundEffect = false;

    private void Awake()
    {
        sr = GetComponent<SpriteRenderer>();
    }
    void Start()
    {
    }

    void Update()
    {

    }

    private void FixedUpdate()
    {
        UpdateBoom();
        UpdateState();
    }

    private void UpdateState()
    {
        GetComponent<SpriteRenderer>().sprite = GetSprite();
        Transform flashBack = transform.Find("FlashBack");
        if (bridgeFloorState == EnumState.Complete)
        {
            GetComponent<Collider2D>().enabled = true;
            if (flashBack != null)
            {
                flashBack.gameObject.SetActive(true);
            }
        }
        else
        {
            GetComponent<Collider2D>().enabled = false;
            if (flashBack != null)
            {
                flashBack.gameObject.SetActive(false);
            }
        }
    }

    public void OnValidateByEditorCall()
    {
        this.OnValidate();
    }
    private void OnValidate()
    {
        UpdateState();
    }

    private Sprite GetSprite()
    {
        Sprite ret = null;
        foreach (Config config in bridgeFloorConfig)
        {
            if (config.bridgeFloorType == bridgeFloorType)
            {
                switch (bridgeFloorState)
                {
                    case EnumState.Complete:
                        ret = config.sprite;
                        break;
                    case EnumState.Booming:
                        ret = config.boomSprite;
                        break;
                    case EnumState.Residual:
                        ret = config.residualSprite;
                        break;
                }
                break;
            }
        }
        return ret;
    }

    private void UpdateBoom()
    {
        if (booming)
        {
            float dt = Time.time - startBoomTime;
            if (dt - completeTime - boomingTime > 0)
            {
                bridgeFloorState = EnumState.Residual;
            }
            else if (dt - completeTime > 0)
            {
                bridgeFloorState = EnumState.Booming;
            }
            else
            {
                bridgeFloorState = EnumState.Complete;
            }

            if (boomPrefab != null)
            {
                int needBoomCount = Mathf.FloorToInt(dt / boomIntervalTime) + 1;
                for (int i = haveBoomCount; i < needBoomCount && i < boomCount; i++)
                {
                    Vector3 position = new Vector3(Random.Range(0, 2), Random.Range(0, 2), 0);
                    Instantiate(boomPrefab, transform.position + position, Quaternion.identity);
                }
                haveBoomCount = needBoomCount;
                if (!playedDeathSoundEffect )
                {
                    Constant.Sounds.EnemyDeath2.PlayOneShot();
                    playedDeathSoundEffect = true;
                }
            }
        }
    }
    public void StartBoom()
    {
        if (!booming)
        {
            booming = true;
            startBoomTime = Time.time;
        }
    }
}
